package com.black.quick.service;

import com.black.quick.dao.OrderDao;
import com.black.quick.domain.OrderInfo;
import com.black.quick.domain.QuickOrder;
import com.black.quick.domain.QuickUser;
import com.black.quick.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author shy_black
 * @date 2019/7/27 13:21
 * @Description:
 */
@Service
public class OrderService {
    @Autowired
    OrderDao orderDao;

    public QuickOrder getQuickOrderByUserIdGoodsId(long userId, long goodsId) {
        return orderDao.getQuickOrderByUserIdGoodsId(userId, goodsId);
    }

    @Transactional
    public OrderInfo createOrder(QuickUser user, GoodsVo goods) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCreateDate(new Date());
        orderInfo.setDeliveryAddrId(0L);
        orderInfo.setGoodsCount(1);
        orderInfo.setGoodsId(goods.getId());
        orderInfo.setGoodsName(goods.getGoodsName());
        orderInfo.setGoodsPrice(goods.getQuickPrice());
        orderInfo.setOrderChannel(1);
        orderInfo.setStatus(0);
        orderInfo.setUserId(user.getId());
        long orderId = orderDao.insert(orderInfo);
        QuickOrder miaoshaOrder = new QuickOrder();
        miaoshaOrder.setGoodsId(goods.getId());
        miaoshaOrder.setOrderId(orderId);
        miaoshaOrder.setUserId(user.getId());
        orderDao.insertQuickOrder(miaoshaOrder);
        return orderInfo;
    }
}
